<?php

function transSingleLongStringFromJinShan($content, $from = 'zh', $to = 'en') {
    if (mb_strlen($content, 'UTF-8') > 2000) {
        //分多次翻译
        $flag = true;
        $intCutCount = ceil(mb_strlen($content, 'UTF-8') / 2000);
    } else {
        $flag = false;
        $intCutCount = 0;
    }
    $post_url = 'http://dict-mobile.iciba.com/fy/api.php';
    if (!$flag) {
        $data = array(
            'version' => 'client.2013.01.15.025',
            'from' => $from,
            'to' => $to,
            'q' => $content
        );
        $result = json_decode(post($post_url, $data), true);
        $transString = '';
        if (!$result['trans_result'] || !$result || !is_array($result) || !is_array($result['trans_result'])) {
            log::w('Trans Failed From JinShan-Baidu');
            return false;
        }
        foreach ($result['trans_result'] as $v) {
            $transString.=$v['dst'] . PHP_EOL;
        }
    } else {
        log::n('Event $intCutCount:' . $intCutCount);
        $transString = '';
        for ($i = 0; $i < $intCutCount; $i++) {
            log::n('Event $intCutCount:' . $intCutCount . ' Process:' . $i);
            $data = array(
                'version' => 'client.2013.01.15.025',
                'from' => $from,
                'to' => $to,
                'q' => mb_substr($content, $i * 2000, 2000, 'UTF-8')
            );

            $result = json_decode(post($post_url, $data), true);
            if (!$result['trans_result'] || !$result || !is_array($result) || !is_array($result['trans_result'])) {
                log::w('Trans Failed From Baidu');
                return false;
            }
            foreach ($result['trans_result'] as $v) {
                $transString.=$v['dst'] . PHP_EOL;
            }
        }
    }

    return $transString;
}

/**
 * 翻译多个长文本
 * @param type $arrDataString
 * @return boolean|string
 */
function transBatchLongStringFromJinShan($arrDataString, $from = 'zh', $to = 'en') {
    ksort($arrDataString);
    $arrIntKv = array();
    $fromTransString = '';
    $intKey = 0;
    foreach ($arrDataString as $strKey => $strV) {
        $fromTransString.=$strV . '|@#@|' . "\n";
        $arrIntKv[$intKey] = $strKey;
        $intKey++;
    }
    $transResultString = transSingleLongStringFromJinShan($fromTransString, $from, $to);
    if (!$transResultString) {
        return false;
    }

    $transResultArray = explode('|@#@|', $transResultString);

    $arrResult = array();

    foreach ($transResultArray as $intKey => $v) {
        if ($arrIntKv[$intKey]) {
            $arrResult[$arrIntKv[$intKey]] = trim($v) . "\n";
        }
    }

    return $arrResult;
}

/**
 * 翻译多个字段,每个字段内都会去除回车换行
 * @param type $arrData
 * @return string|boolean
 */
function transBatchDataNoEOLFromJinShan($arrData, $from = 'zh', $to = 'en') {
    foreach ($arrData as &$v) {
        $v = trim($v);
        $v = str_replace("\n", '', $v);
        $v = str_replace(PHP_EOL, '', $v);
        $v = addTransBatchEmptyNull($v);
    }unset($v);

    ksort($arrData);
    $arrIntKv = array();
    $fromTransString = '';
    $intKey = 0;
    foreach ($arrData as $strKey => $strV) {
        $arrIntKv[$intKey] = $strKey;
        $intKey++;
    }
    $content = $fromTransString = implode("\n", $arrData);

    if (!$content) {
        log::e('Trans Error:Empty $content = $fromTransString');
        return '';
    }
    $post_url = 'http://dict-mobile.iciba.com/fy/api.php';
    $data = array(
        'version' => 'client.2013.01.15.025',
        'from' => $from,
        'to' => $to,
        'q' => $content
    );
    $arrTransResult = json_decode(post($post_url, $data), true);

    if (!isset($arrTransResult["trans_result"]) || empty($arrTransResult["trans_result"]) || !is_array($arrTransResult["trans_result"])) {
        log::w('Trans Error:Empty $arrTransResult["trans_result"]');
        sleep(7);
        var_dump($arrTransResult);
        return false;
    }

    $transResult = $arrTransResult["trans_result"];
    $arrReturnResult = array();

    foreach ($transResult as $intKey => $v) {
        if ($arrIntKv[$intKey]) {
            $arrReturnResult[$arrIntKv[$intKey]] = deleteTransBatchEmptyNull($v['dst']);
        }
    }

    return $arrReturnResult;
}

function transLongStringFromBaidu($content) {
    return transSingleLongStringFromBaidu($content);
}

function transLongStringFromJinShan($content) {
    return transSingleLongStringFromJinShan($content);
}

/**
 * 翻译单个长文本
 * @param type $content
 * @return string|boolean
 */
function transSingleLongStringFromBaidu($content) {
    if (mb_strlen($content, 'UTF-8') > 2000) {
        //分多次翻译
        $flag = true;
        $intCutCount = ceil(strlen($content) / 6000);
    } else {
        $flag = false;
        $intCutCount = 0;
    }
    if (!$content) {
        return '';
    }
    $post_url = 'http://openapi.baidu.com/public/2.0/bmt/translate';
    if (!$flag) {
        $data = array(
            'client_id' => 'nxvDV5QLplKadGYInZzvjvr0',
            'from' => 'zh',
            'to' => 'en',
            'q' => $content
        );
        $result = json_decode(post($post_url, $data), true);
        $transString = '';
        if (!$result['trans_result'] || !$result || !is_array($result) || !is_array($result['trans_result'])) {
            log::w('Trans Failed From Baidu');
            return false;
        }
        foreach ($result['trans_result'] as $v) {
            $transString.=$v['dst'] . PHP_EOL;
        }
    } else {
        log::n('Event $intCutCount:' . $intCutCount);
        $transString = '';
        for ($i = 0; $i < $intCutCount; $i++) {
            log::n('Event $intCutCount:' . $intCutCount . ' Process:' . $i);
            $data = array(
                'client_id' => 'nxvDV5QLplKadGYInZzvjvr0',
                'from' => 'zh',
                'to' => 'en',
                'q' => mb_substr($content, $i * 2000, 2000, 'UTF-8')
            );

            $result = json_decode(post($post_url, $data), true);
            if (!$result['trans_result'] || !$result || !is_array($result) || !is_array($result['trans_result'])) {
                log::w('Trans Failed From Baidu');
                return false;
            }
            foreach ($result['trans_result'] as $v) {
                $transString.=$v['dst'] . PHP_EOL;
            }
        }
    }
    return $transString;
}

/**
 * 翻译多个长文本
 * @param type $arrDataString
 * @return boolean|string
 */
function transBatchLongStringFromBaidu($arrDataString) {
    ksort($arrDataString);
    $arrIntKv = array();
    $fromTransString = '';
    $intKey = 0;
    foreach ($arrDataString as $strKey => $strV) {
        $fromTransString.=$strV . '|@#@|' . "\n";
        $arrIntKv[$intKey] = $strKey;
        $intKey++;
    }
    $transResultString = transSingleLongStringFromBaidu($fromTransString);
    if (!$transResultString) {
        return false;
    }

    $transResultArray = explode('|@#@|', $transResultString);

    $arrResult = array();

    foreach ($transResultArray as $intKey => $v) {
        if ($arrIntKv[$intKey]) {
            $arrResult[$arrIntKv[$intKey]] = trim($v) . "\n";
        }
    }

    return $arrResult;
}

/**
 * 翻译多个字段,每个字段内都会去除回车换行
 * @param type $arrData
 * @return string|boolean
 */
function transBatchDataNoEOLFromBaidu($arrData) {
    foreach ($arrData as &$v) {
        $v = trim($v);
        $v = str_replace("\n", '', $v);
        $v = str_replace(PHP_EOL, '', $v);
        $v = addTransBatchEmptyNull($v);
    }unset($v);

    ksort($arrData);
    $arrIntKv = array();
    $fromTransString = '';
    $intKey = 0;
    foreach ($arrData as $strKey => $strV) {
        $arrIntKv[$intKey] = $strKey;
        $intKey++;
    }
    $content = $fromTransString = implode("\n", $arrData);

    if (!$content) {
        log::e('Trans Error:Empty $content = $fromTransString');
        return '';
    }
    $post_url = 'http://openapi.baidu.com/public/2.0/bmt/translate';
    $data = array(
        'client_id' => 'nxvDV5QLplKadGYInZzvjvr0',
        'from' => 'zh',
        'to' => 'en',
        'q' => $content
    );
    $arrTransResult = json_decode(post($post_url, $data), true);

    if (!isset($arrTransResult["trans_result"]) || empty($arrTransResult["trans_result"]) || !is_array($arrTransResult["trans_result"])) {
        log::w('Trans Error:Empty $arrTransResult["trans_result"]');
        sleep(7);
        var_dump($arrTransResult);
        return false;
    }

    $transResult = $arrTransResult["trans_result"];
    $arrReturnResult = array();

    foreach ($transResult as $intKey => $v) {
        if ($arrIntKv[$intKey]) {
            $arrReturnResult[$arrIntKv[$intKey]] = deleteTransBatchEmptyNull($v['dst']);
        }
    }

    return $arrReturnResult;
}

function addTransBatchEmptyNull($s) {
    if (!$s || empty($s)) {
        $s = '|@#@|';
    }
    return $s;
}

function deleteTransBatchEmptyNull($s) {
    return str_replace('|@#@|', '', $s);
}

?>